<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="业务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-if="modalOpera==1" v-decorator="['id', {rules: [{required: true}]}]" />
        </a-form-item>
        <a-form-item label="站点名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['siteName', {rules: [{required: true, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>

        <a-form-item label="所属组织" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tree-select
            v-decorator="['orgId', {rules: [{required: true, message: '请选择所属组织'}]}]"
            :treeData="treeData"
            :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
            placeholder="请选择所属组织"
            allowClear
            treeDefaultExpandAll
          ></a-tree-select>
        </a-form-item>

        <a-form-item label="共享至组织" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择是否共享"
            v-decorator="[
              'isShareOrg',
              {rules: [{ required: true, message: '请选择是否共享组织内成员'}],initialValue:'1'}
            ]"
          >
            <a-select-option key="1" value="1" >共享</a-select-option>
            <a-select-option key="0" value="0" >私有</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="站点副标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['siteSubtitle', {rules: [{required: true, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>

        <a-form-item label="站点LOGO" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['siteLogo', {rules: [{required: true, min: 3, message: '请输入至少三个字符的规则！'}]}]" /> -->
          <upload-file ref="uploadFile"></upload-file>
        </a-form-item>

        <a-form-item label="域名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="不包含http://" v-decorator="['domain', {rules: [{required: true, min: 5, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>
        <a-form-item label="关键字" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <tag ref="mytag"></tag>
        </a-form-item>
        <a-form-item label="站点描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['siteDesc', {rules: [{required: false, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>
        <a-form-item label="备案信息" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['siteicp', {rules: [{required: false, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>
        <a-form-item label="站点版权" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['copyright', {rules: [{required: false, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>
        <a-form-item
          label="统计代码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['statisticalCode', {rules: [{required: false, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>

        <a-form-item
          label="评论代码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['commentCode', {rules: [{required: false, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>

        <a-form-item
          label="分享代码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['shareCode', {rules: [{required: false, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    save,
    getById,
    update,
    pageOrg
  } from '@/api/site/site'
  import tag from '@/components/Tags'
  import uploadFile from './uploadFile.vue'
  import pick from 'lodash.pick'
  export default {
    components: {
      tag,
      uploadFile
    },
    data () {
      return {
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 13
          }
        },
	      treeData: [],
        modalTitle: '',
        modalOpera: 0, // 0：新增 1：编辑
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    created () { this.loadTreeData() },
    methods: {
      add () {
        this.modal('新建站点', 0)
      },
      edit (record) {
        this.modal('编辑站点', 1)
        getById({
          id: record.id
        }).then(response => {
          // console.log(response);
          const result = response.res
          if (result.siteKeyWords) {
                    this.$refs.mytag.setTags(result.siteKeyWords)
          }
          if (result.siteLogo) {
            this.$refs.uploadFile.setValue(JSON.parse(result.siteLogo))
          }
         this.form.setFieldsValue(pick(result, 'id', 'siteName', 'siteSubtitle', 'siteKeyWords', 'siteDesc',
           'siteLogo', 'domain', 'siteicp', 'copyright', 'commentCode', 'shareCode', 'statisticalCode', 'orgId', 'isShareOrg'
         ))
         const share = result.isShareOrg ? '1' : '0'
         this.form.setFieldsValue({ 'isShareOrg': share })
        })
      },
      modal (title, opera) {
        this.modalTitle = title
        this.modalOpera = opera
        this.visible = true
        this.form.resetFields()
      },
      loadTreeData () {
        pageOrg({}).then(response => {
          this.treeData = response.res.children
        })
      },
      handleSubmit () {
        const {
          form: {
            validateFields
          }
        } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            const logo = this.$refs.uploadFile.getValue()
            if (logo) {
              values.siteLogo = JSON.stringify(logo)
            }
            const tags = this.$refs.mytag.getTagStr()
            if (tags) {
              values.siteKeyWords = tags
            }
            values.isShareOrg = parseInt(values.isShareOrg)
            if (this.modalOpera === 0) {
              save(values).then(response => {
                this.message(response)
              })
            } else if (this.modalOpera === 1) {
              update(values).then(response => {
                this.message(response)
              })
            }
          }
          this.confirmLoading = false
        })
      },
      message (response) {
        if (response.code === 0) {
          this.$message.info(response.msg)
          this.$emit('ok')
          this.handleCancel()
        } else {
          this.$message.error(response.msg)
        }
      },
      handleCancel () {
        this.visible = false
        this.confirmLoading = false
        this.$refs.mytag.clearTags()
        this.$refs.uploadFile.clear()
      }
    }
  }
</script>
